﻿Imports DTO
Imports BUS
Imports DevExpress.XtraGrid.Views.Grid

Public Class ucKhachHang

    Dim kh As New KhachHangDTO

    Private Sub FilterControl1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub barbtnThemKH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnThemKH.ItemClick
        Dim frm As New frmThemKhachHang
        AddHandler frm.LamMoiNVKhiThem, AddressOf LayDSKhachHang
        frm.ShowDialog()
    End Sub

    Sub LayDSKhachHang()
        GCKhuVuc.DataSource = KhachHangBUS.LayDSKhachHang()
    End Sub



    Private Sub ucKhachHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barbtnSuaChuaKH.Enabled = False
        barbtnXoaKH.Enabled = False
        barbtnXuatKH.Enabled = False
        LayDSKhachHang()
    End Sub

    Private Sub barbtnNapLaiKH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnNapLaiKH.ItemClick
        LayDSKhachHang()
    End Sub

    Private Sub barbtnSuaChuaKH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnSuaChuaKH.ItemClick
        Dim frm As New frmThemKhachHang
        frm.tenKh = kh.TenKhachHang
        frm.maKh = kh.MaKhuVuc
        frm.dc = kh.DiaChi
        frm.dd = kh.DiDong
        frm.dt = kh.DienThoai
        frm.email = kh.Email
        frm.fax = kh.Fax
        frm.maKh = kh.MaKhachHang
        frm.mst = kh.MaSoThue
        frm.conQL = kh.MaTrangThai
        frm.nh = kh.NganHang
        frm.skype = kh.NickSkype
        frm.yahoo = kh.NickYahoo
        frm.web = kh.TrangWEB
        frm.tk = kh.TaiKhoan
        frm.kt = 1
        frm.Text = "Sửa Khách Hàng"
        frm.ShowDialog()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        If e.RowHandle >= 0 Then
            barbtnSuaChuaKH.Enabled = True
            barbtnXoaKH.Enabled = True
            kh.TenKhachHang = GridView1.GetRowCellValue(e.RowHandle, GCTenKH).ToString()
            kh.MaKhuVuc = GridView1.GetRowCellValue(e.RowHandle, GCKhuVucKH).ToString()
            kh.DiaChi = GridView1.GetRowCellValue(e.RowHandle, GCDiaChiKH).ToString()
            kh.DiDong = GridView1.GetRowCellValue(e.RowHandle, GCDiDongKH).ToString()
            kh.DienThoai = GridView1.GetRowCellValue(e.RowHandle, GCDienThoaiKH).ToString()
            kh.Email = GridView1.GetRowCellValue(e.RowHandle, GCEmailKH).ToString()
            kh.Fax = GridView1.GetRowCellValue(e.RowHandle, GCFaxKH).ToString()
            kh.MaKhachHang = GridView1.GetRowCellValue(e.RowHandle, GCMaKH).ToString()
            kh.MaSoThue = GridView1.GetRowCellValue(e.RowHandle, GCMSTKH).ToString()
            kh.MaTrangThai = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCConQuanLyKV))
            kh.NganHang = GridView1.GetRowCellValue(e.RowHandle, GCTenNHKH).ToString()
            kh.NickSkype = GridView1.GetRowCellValue(e.RowHandle, GCNickSkyKH).ToString()
            kh.NickYahoo = GridView1.GetRowCellValue(e.RowHandle, GCNickYahooKH).ToString()
            kh.TrangWEB = GridView1.GetRowCellValue(e.RowHandle, GCWebsiteKH).ToString()
            kh.TaiKhoan = GridView1.GetRowCellValue(e.RowHandle, GCSTKKH).ToString()
        End If
    End Sub

    Private Sub barbtnXoaKH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnXoaKH.ItemClick
        Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 18)
        If pq.Xoa = True Then
            Try
                If KhachHangBUS.XoaKhachHang(kh) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thành công khách hàng", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    LayDSKhachHang()
                    HeThongBUS.themNhatKy(frmMain.maND, "Quản lý khách hàng", "Xóa", kh.MaKhachHang)
                End If
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thất bại, khách hàng này đang nằm trong 1 đơn hàng khủng nào đó mà chưa thanh toán", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                KhachHangBUS.DongKetNoi.Close()
            End Try
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub GridView1_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridView1.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub

    Private Sub GridView1_RowStyle(ByVal sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles GridView1.RowStyle
        If (e.RowHandle >= 0) Then
            Dim category As Integer = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCConQuanLyKV))
            If category = 0 Then
                e.Appearance.ForeColor = Color.Red
                e.Appearance.FontStyleDelta = FontStyle.Italic
            End If
        End If
    End Sub
End Class
